package org.i1510.common.interceptor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.i1510.common.bean.SessionUserBean;
import org.i1510.common.service.ISysRolePopService;
import org.i1510.common.utils.KBProperties;
import org.i1510.common.utils.PrintWriterUtils;
import org.i1510.common.utils.SpringContextUtil;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.encryption.Md5;
import org.i1510.common.utils.web.ResultMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;

public class AuthAccessInterceptor extends BaseInterceptor {

	private static Logger logger = LoggerFactory
			.getLogger(AuthAccessInterceptor.class);

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		if (isAllow(request)) {
			return true;// 不拦截直接通过
		}

		ResultMapper mapper = ResultMapper.generateFailure();
		ISysRolePopService iSysRolePopService = (ISysRolePopService) SpringContextUtil
				.getBean("iSysRolePopService");

//		 (java.lang.Class<T>) class org.springframework.web.method.HandlerMethod
//		 (java.lang.Class<T>) class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
		HandlerMethod handlerM = null;
		if(handler instanceof HandlerMethod ){
			handlerM = (HandlerMethod) handler;
		}else{
			return false;
		}
		
		 
		String javaClass = handlerM.getMethod().getDeclaringClass().getName();
		String javaMethod = handlerM.getMethod().getName();

		
		boolean flag = false;
		PrintWriterUtils pr = new PrintWriterUtils();
		SessionUserBean userSession = UserUtils.getSessionUserBean(request);
		if (null == userSession) { //未登录
			//不允许通过
		}else{
			//判断超管
			if(null != userSession.getSysUser().getFdRoleId() && userSession.getSysUser().getEmail() != null
					&& Md5.getPassword(userSession.getSysUser().getEmail()).equals(KBProperties.SYSSUPERADMIN)){
				return true;
			}
			// 获取权限
			List list = iSysRolePopService.getRole2JavaClass(userSession.getSysUser().getFdRoleId(), javaClass);
			if(list.size() == 0){
				//不允许通过
			}else{
				for (int i = 0; i < list.size(); i++) {
					Map map = (Map)list.get(i);
					if(map.get("javaMethod").toString().contains("|"+javaMethod)){
						//允许通过
						flag = true;
						break;
					}else if(map.get("javaMethod").toString().equals("**")){//拥有该javaClass下所有权限
						//允许通过
						flag = true;
						break;
					}
				}
				
			}
		}
		
		if(flag){
			//通过
			return true;
		}else{
			//不允许通过
			mapper.appendResult("message", "权限不足,请找管理员!");
			pr.sendPrintWriterUtils(response, mapper.toJson(), "plain");
			return false;
		}
		

		
	}

}